'''
Created on Sep 6, 2011

@author: Zhao, Qi
'''

import oursql, DBUtils
from DBUtils.PooledDB import PooledDB
from common.config import *
from common import server_log

class DBPool:
    def __init__(self,
                 db_driver,
                 maxconnections,
                 blocking,
                 host,
                 port,
                 user,
                 passwd,
                 db,
                 use_unicode):
        self.dbpool = PooledDB(db_driver,
                               maxconnections = maxconnections,
                               blocking = blocking,
                               host = host,
                               port = port,
                               user = user,
                               passwd = passwd,
                               db = db,
                               use_unicode = use_unicode)
    
    def connection(self):
        self.conn = Connection(self.dbpool.connection())
        return self.conn


class Connection:
    def __init__(self, con):
        self.conn = con
        
    def cursor(self, cursor_type = None):
        if cursor_type:
            self.cur = Cursor(self.conn.cursor(cursor_type))
        else:
            self.cur = Cursor(self.conn.cursor())
        return self.cur
    
    def commit(self):
        self.cur = self.conn.commit()
    
    def rollback(self):
        self.cur = self.conn.rollback()
    
    def close(self):
        self.cur = self.conn.close()
    
    
class Cursor:
    def __init__(self, curs):
        self.cur = curs
    
    def __iter__(self):
        return self.cur.__iter__()
    
    def _log_sql(self, sql_str, params, is_multi = False):
        if is_multi:
            server_log.debug(("[SQL] %s" % sql_str, "[SQL PARAMS] %s" % str(params)))
        else:
            if params:
                sql_str = sql_str.replace('?','%s') % params
            server_log.debug("[SQL] %s" % sql_str)
        
    def execute(self, sql_str, params = None):
        self._log_sql(sql_str, params)
        if params:
            self.cur.execute(sql_str, params)
        else:
            self.cur.execute(sql_str)
        self.lastrowid = self.cur.lastrowid
        self.rowcount = self.cur.rowcount
        
    def executemany(self, sql_str, params):
        self._log_sql(sql_str, params, True)
        self.cur.executemany(sql_str, params)
        self.lastrowid = self.cur.lastrowid
        self.rowcount = self.cur.rowcount
    
    def nextset(self):
        return self.cur.nextset()
    
    def fetchone(self):
        return self.cur.fetchone()
    
    def fetchmany(self, size=None):
        if size:
            return self.cur.fetchmany(size)
        else:
            return self.cur.fetchmany()
        
    def fetchall(self):
        return self.cur.fetchall()
        
    def close(self):
        self.cur.close()


MASTER_DBPOOL = DBPool(oursql,
                       maxconnections = MASTER_MAX_CONN,
                       blocking = 1,
                       host = MASTER_DB_ADDR,
                       port = MASTER_DB_PORT,
                       user = MASTER_USER,
                       passwd = MASTER_PASSWD,
                       db = MASTER_DB_NAME,
                       use_unicode = False)

SLAVE_DBPOOL = DBPool(oursql,
                      maxconnections = SLAVE_MAX_CONN,
                      blocking = 1,
                      host = SLAVE_DB_ADDR,
                      port = SLAVE_DB_PORT,
                      user = SLAVE_USER,
                      passwd = SLAVE_PASSWD,
                      db = SLAVE_DB_NAME,
                      use_unicode = False)


if __name__ == "__main__" :
    SQL_NEW_DEAL = '''
    SELECT * FROM LU_Status
    '''
    conn = SLAVE_DBPOOL.connection()
    cursor = conn.cursor(oursql.DictCursor)
    cursor.execute(SQL_NEW_DEAL)
    for row in cursor:
        print row['desc']
